package com.conwax.gmap;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;

import com.conwax.gmap.event.GEventHandler;
import com.conwax.gmap.event.GEventListener;
import com.conwax.gmap.event.GEventSource;

public class GEvent implements Serializable {

	private static final long serialVersionUID = 1L;
	
	Map<Integer, Event> events = new HashMap<Integer, Event>();

	public static GEventListener addListener(GEventSource source, GEventHandler handler) {
		int listenerId = Session.get().nextSequenceValue();
		//events.put(listenerId, new Event(listenerId, source, listener));
		//source.addListener(handler);
		return null;
	}

	public void removeListener(int listenerId) {
		events.remove(listenerId);
	}

	private class Event {
		public final int listenerId;
		public final GEventSource gEventSource;
		public final GEventHandler gEventListener;

		public Event(int listenerId, GEventSource source,
				GEventHandler listener) {
			this.listenerId = listenerId;
			this.gEventSource = source;
			this.gEventListener = listener;
		}
	}

//	@Override
//	public String getJSInit() {
//		StringBuffer sb = new StringBuffer();
//		for (Integer listenerId : events.keySet()) {
//			Event event = events.get(listenerId);
//			sb.append("WicketConGoogle.maps[" + event.gEventSource.getId()
//					+ "].addListener(\"" + event.listenerId + "\", \""
//					+ event.gEventListener.getEvent() + "\");");
//		}
//		return sb.toString();
//	}
//
//	void onEvent(AjaxRequestTarget target, int listenerId) {
//		events.get(listenerId).gEventListener.onEvent(target);
//	}
}
